/**
 * Created by wwh on 2017/7/17.
 */
/**
 * 时间格式取年月日
 * @param time
 * @returns {*}
 */
function getYearMonthDate(time) {
    if(time != null) {
        var YMD = time.split(" ")[0];  // 年月日
        var HMS = time.split(" ")[1];  // 时分秒
        return YMD;
    }
    return null;
}
layui.config({
    base: '../../static/js/layuiModulesEX/'
});
layui.use(['paging','form'],function () {
    var $ = layui.jquery,
       paging = layui.paging(),
       layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
       layer = layui.layer,
       laytpl = layui.laytpl,
       form = layui.form();

    // var map = new BMap.Map("bdMap"); //创建地图实例
    // $('#bdMap').hide();               //由于和Vue有冲突，show==true时隐藏地图

    var vm = new Vue({
        el:'#Vue',
        data:{
            show:true,
            studentInfo:{
                userid:userid
            },
            user:{
                id:userid
            },
            recruitInfo:{
                // orgid:""
            }
            ,orgInfo:{
                id:""
                ,longtitude:""
                ,latitude:""
            }
            ,orginfoFullName:""
            ,positionType:{ }
        },
        methods:{
            // getMapInfo:function () {
            //     if(vm.show==true){
            //         $("#bdMap").remove();
            //     }
            //     console.log(vm.orgInfo);
            //     //创建地图点坐标
            //     var point = new BMap.Point(vm.orgInfo.longtitude, vm.orgInfo.latitude);
            //     //设置地图中心点和地图级别
            //     map.centerAndZoom(point, 15);
            //     //设置标注
            //     var marker = new BMap.Marker(point);
            //     //清除所有标注
            //     map.clearOverlays();
            //     //添加标注
            //     map.addOverlay(marker);
            //     //启用鼠标滚轮放大缩小
            //     map.enableScrollWheelZoom(true);
            // },
            /**
             * 查询按钮
             */
            search:function () {
                paging.get({
                    title:$('#searchVal').val()
                });
            },
            /**
             * 返回按钮
             */
            reload:function () {
                $('#bdMap').hide();
                vm.show = true;
            }
        }
    });
    // vm.getMapInfo();

    // 获取studentInfo为了筛选该学生所在学院的招聘信息
    axios.get('/student/findDetail/'+userid)
        .then(function (response) {
            // console.log(response.data);
            vm.studentInfo = response.data.data.studentInfo;
            console.log(vm.studentInfo);
        })
        .catch(function (response) {
            console.log(response);
        })
    
    paging.init({
        openWait: true,
        url:'/recruitInfo/findByCondition2',
        elem: '#content', //内容容器
        params: {
            state:"3",
            schoolid:vm.studentInfo.schoolid
        },
        contentType : "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success:function (Response) {
            // alert("渲染成功");
        },
        fail:function (msg) {
            // alert("获取数据失败");
        },
        complate:function () {
            // alert('处理完成');
            //绑定按钮
            $('#content').children('tr').each(function () {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var $org = $(this).children('td:nth-child(2)');
                var id = $that.children('a[data-opt=more]').data('id');
                var orgid = $org.children('a[data-opt=company]').data('id');
                // console.log(id);
                // console.log(orgid);
                /**
                 * 详情按钮
                 */
                $that.children('a[data-opt=more]').on('click', function() {
                    axios.get('/recruitInfo/find/'+id)
                        .then(function (response) {
                            $('#environment').attr('src','');
                            console.log(response.data);
                            vm.recruitInfo = response.data.data.recruitInfo;
                            if (vm.recruitInfo.workingpic != null&&vm.recruitInfo.workingpic != 0) {
                                axios.get('/image/find/'+vm.recruitInfo.workingpic)
                                    .then(function (res) {
                                        console.log(res.data.data);
                                        $('#environment').attr('src', res.data.data);
                                    })
                                    .catch(function (msg) {
                                        console.log(msg);
                                    })
                            }
                            //弹框
                            layer.open({
                                type:1,
                                title:"招聘详情",
                                content:laytpl($('#detailTpl').html()).render(vm.recruitInfo),
                                shade: [0.5, '#FFF'], //模态框
                                area: ['700px', '600px'],
                                zIndex: 19950924,
                                btn:['返回'],
                                success:function () {
                                    var divshow = $("#job");
                                    divshow.text('');
                                    divshow.html(vm.recruitInfo.job);
                                },
                                yes:function (index) {
                                    layer.close(index);
                                }
                            });
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                });
                /**
                 * 企业链接
                 */
                $org.children('a[data-opt=company]').on('click',function () {
                    // console.log(orgid);
                    location.href="/student/orgInfo?origin='/student/recruitInfo'&orgId="+orgid+"&operateType=2";
                });
                /**
                 * 应聘按钮
                 */
                $that.children('a[data-opt=apply]').on('click',function () {
                    $.get('/student/selectCV',null,function (html) {
                        layer.open({
                            type:1,
                            title:"选择简历",
                            area:['700px','300px'],
                            content:html,
                            success:function (layero,index) {
                                form.on('submit(selectCV)',function (data) {
                                    console.log(data.field);
                                    layer.confirm("确认应聘吗？",function (index1) {
                                        var jobApply = {
                                            recruitinfoid:id,
                                            employerid:orgid,
                                            stid:userid,
                                            cvid:data.field.CVid,
                                            status:1
                                        };
                                        console.log(jobApply);
                                        axios.post('/jobApply/addOne',jobApply)
                                            .then(function (response) {
                                                console.log(response);
                                                if(response.data.success){
                                                    layer.msg("已投递简历",{icon:1,time:1000});
                                                }
                                                else{
                                                    layer.msg(response.data.message);
                                                }
                                            })
                                            .catch(function (msg) {
                                                console.log(msg);
                                            })
                                        layer.close(index1);
                                        layer.close(index);
                                    });
                                    return false;
                                });
                                return false;
                            },
                            end:function () {
                            }
                        });
                    });
                });
            });
        }
    });

    //搜索框
    $('#search').on('click',function () {
       paging.get({
           title:$('#searchVal').val()
       });
       alert('你点击了搜索按钮');
    });


});
